package ru.urfc.portal.model.accounting;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import ru.urfc.portal.model.SpectrObjectWithContragent;

@Table(name = "portal_payments")
@Entity
public class Payment extends SpectrObjectWithContragent implements BalanceOperation {
	private Invoice invoice;
	private String number;
	@Temporal(TemporalType.DATE)
	private Date date;
	@Column(scale = 2, precision = 16)
	private BigDecimal amount;
	
	public Payment() {
	}

	public Payment(Contragent contragent, Invoice invoice, String number,
			Date date, BigDecimal amount) {
		super(contragent);
		this.invoice = invoice;
		this.number = number;
		this.date = date;
		this.amount = amount;
	}

	@Override
	public BigDecimal getAmountWithSign() {
		return amount;
	}
	
	@Override
	public Date getDate() {
		return date;
	}

	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyy");

	@Override
	public String getName() {
		String invoiceName = invoice == null ? "---" : invoice.getNumber();
		String invoiceDate = invoice == null ? "---" : DATE_FORMAT.format(invoice.getDate());
		return MessageFormat.format("Платеж за услуги, п/п №{0} по счету №{1} от {2}",
				number, invoiceName, invoiceDate);
	}
	
}
